package com.hucc.sqlitedemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by l on 2017/5/21.
 */

public class MyBaseAdapter extends BaseAdapter {

    private Context context;
    private List<Person>  list;

    public MyBaseAdapter(Context context, List<Person> totalList) {
        this.context = context;
        this.list = totalList;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.txt_id = (TextView) convertView.findViewById(R.id.tv_id);
            holder.txt_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.txt_phone = (TextView) convertView.findViewById(R.id.tv_phone);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.txt_id.setText(list.get(position).get_id()+"");
        holder.txt_name.setText(list.get(position).getName());
        holder.txt_phone.setText(list.get(position).getPhone()+"");
        return convertView;
    }

    public class ViewHolder{
        TextView txt_id;
        TextView txt_name;
        TextView txt_phone;
    }
}
